#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
void scan(){
    const double cPi = 3.1415926535898;
    const double cDeg2Rad = cPi / 180;
    for (int i = 0; i < 6; i++)
    {   
        double theta = i*0.5;
        for (int j = 0; j < 1; j++){
            double alpha = 0;
            for (int xi = 1; xi < 9; xi++){
                int  momentum= xi * 5;
                //for (int yi = 0; yi < 7; yi++){
                    //int y = yi * 5;
                    ofstream ofFile(Form("/home/jxm/geant4.10.06.02/desyMC _new/scan/run%d.mac",8*i + 1*j + xi), ios::trunc);
                    double dirX = sin(cPi-theta * cDeg2Rad) * cos(-cPi/2+alpha *cDeg2Rad );
                    double dirY = sin(cPi-theta * cDeg2Rad) * sin(-cPi/2+alpha *cDeg2Rad );
                    double dirZ = cos(cPi-theta * cDeg2Rad);
                    double px=dirX*momentum;
                    double py=dirY*momentum;
                    double pz=dirZ*momentum;

                    ofFile << "/control/verbose 0" << endl; 
                    ofFile << "/run/verbose 0" << endl; 
                    ofFile << "/vis/verbose 0" << endl; 
                    ofFile << "/process/verbose 0" << endl; 
                    ofFile << "/tracking/verbose 0" << endl; 

                    ofFile << "/MyRun/SetG4BasedFileName "
                           << "px_" << px << "py_" << py << "pz_" << pz << "theta_"<<theta<<".root" << endl;
                    
                    ofFile << "/MyGun/SimpleGun" << endl;
                    ofFile << "/run/initialize" << endl;
                    //ofFile << "/gun/direction " << dirX << " " << dirY << " " << dirZ << endl;
                    ofFile << "/gun/momentum "<<px <<" "<<py<<" "<<pz<<" GeV" << endl;
                    ofFile << "/gun/position 0 0 0 mm" <<endl;
                    ofFile << "/gun/particle mu-" << endl;
                    ofFile << "/run/beamOn 10000" << endl;
                    
                    ofFile.close();
              

                    cout << "mv  8800.root "
                           << "px_" << px << "py_" << py << "pz_" << pz << "theta_"<<theta<<".root" << endl;
                //}
            }
        //     ofstream ofFile(Form("run%d.mac",10 * i + j), ios::trunc);
        //     double dirX = sin(theta * cDeg2Rad) * cos(alpha *cDeg2Rad + cPi / 2);
        //     double dirY = sin(theta * cDeg2Rad) * sin(alpha *cDeg2Rad + cPi / 2);
        // double dirZ = -cos(theta * cDeg2Rad);

        // ofFile << "/gun/direction " << dirX << " " << dirY << " " << dirZ << endl;
        // ofFile << "/gun/energy 5 GeV" << endl;
        // ofFile << "/gun/postion " << "{x} {y} " << 0 << endl;
        // ofFile << "/gun/particle mu-" << endl;
        // ofFile << "/run/beamOn 1000" << endl;
        // ofFile << "/MyRun/SetG4BasedFileName "
        //        << "x_{x}y_{y}"
        //        << "theta_" << theta << "alpha" << alpha << ".root" << endl;
        // ofFile.close();
        }
    }
    return 0;
}
